package com.bilibili.sys.servlet;

import com.bilibili.sys.bean.SysUser;
import com.bilibili.sys.service.IUserService;
import com.bilibili.sys.service.impl.UserServiceImpl;
import com.bilibili.sys.utils.Constant;
import com.bilibili.sys.utils.PageUtils;
import com.bilibili.sys.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author alpha
 * @program: BookSystem
 * @description:
 * @since 2024-03-26 22:58
 **/
@WebServlet(name = "userServlet", urlPatterns = {"/sys/userServlet"})
public class UserServlet extends BaseServlet {
    private IUserService service = new UserServiceImpl();

    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //调用父类中的list方法完成分页数据的处理
        super.list(req,resp);
        //做分页的查询
        service.listPage(pageUtils);
        //还需要查询总共有多少条记录
        int count = service.count(pageUtils);
        //表示查询数据
        System.out.println("------userServlet");
        //查询所有的用户数据
        //List<SysUser> list = service.list(null);
        //System.out.println(list);
        //把查询的用户数据存储在request作用域中
        req.setAttribute(Constant.LIST_PAGE_UTILS, pageUtils);
        //resp.sendRedirect("/main.jsp");
        //通过服务端转发的方式跳转页面
        req.getRequestDispatcher("/sys/user/list.jsp").forward(req, resp);
    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //表示添加数据
        //获取客户端提交的数据
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String nickname = req.getParameter("nickname");
        String img = req.getParameter("img");
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(password);
        sysUser.setNickname(nickname);
        sysUser.setImg(img);
        if(StringUtils.isNotEmpty(id)){
            //表示是更新操作
            sysUser.setId(Integer.parseInt(id));
            service.updateById(sysUser);//更新用户信息
        }else {
            //表示是添加操作
            //调用Service的方法完成数据的存储
            service.save(sysUser);
            System.out.println(sysUser);
        }

        //要做数据的查询
        resp.sendRedirect("/sys/userServlet?action=" + Constant.BASE_ACTION_LIST);//重定向

    }

    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        int count = service.deleteById(Integer.parseInt(id));
        PrintWriter writer = resp.getWriter();
        writer.write(count+"");
        writer.flush();
        writer.close();
    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //需要获取客户端提交的ID信息
        String id = req.getParameter("id");
        if (StringUtils.isNotEmpty(id)) {
            //说明是更新操作 需要根据id查询出用户的具体信息
            SysUser user = service.findById(Integer.parseInt(id));
            req.setAttribute("entity",user);
        }
        req.getRequestDispatcher("/sys/user/addOrUpdate.jsp").forward(req, resp);

    }


}
